package com.alorm.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.test.bo.Etudiant;

public class AlormUtils {

	public static String getClassName(Class pClass) {

		String[] tokens = pClass.getName().trim().split("\\.");

		return tokens[tokens.length - 1];

	}

	public static String getPkName(Class pClass) {
		Field field = pClass.getDeclaredFields()[0];
		String fNom = field.getName();
		return fNom;
	}

	public static String getGetterMethod(String pFieldName) {

		char majChar = Character.toUpperCase(pFieldName.charAt(0));

		return "get" + majChar + pFieldName.substring(1);

	}

	public static String[] getAllFieldsName(Class pClass) {

		Field fields[] = pClass.getDeclaredFields();
		String fieldsName[] = new String[fields.length];

		for (int i = 0; i < fields.length; i++) {
			fieldsName[i] = fields[i].getName();
		}

		return fieldsName;
	}

	public static Map<String, String> getAllFieldsValue(Object pObject) throws NoSuchMethodException,
			SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

		Class lClass = pObject.getClass();
		Map<String, String> fieldsValue = new HashMap<String, String>();
		Field fields[] = lClass.getDeclaredFields();
		String fieldsName[] = new String[fields.length];
		Method method = null;
		// number = (Number)

		for (int i = 0; i < fields.length; i++) {

			method = lClass.getMethod(getGetterMethod(fields[i].getName()));

			fieldsValue.put(fields[i].getName(), String.valueOf(method.invoke(pObject)));
		}

		return fieldsValue;
	}

	public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {

	}
}
